package cn.sc.summer.adapter.controller.login;

import cn.sc.summer.constant.model.Result;
import cn.sc.summer.domain.login.SysLoginService;
import cn.sc.summer.protocol.form.login.AppLoginForm;
import cn.sc.summer.protocol.form.login.LoginForm;
import cn.sc.summer.protocol.form.login.SmsCodeLoginForm;
import cn.sc.summer.token.constant.LoginAPIConstant;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Map;

/**
 * 类名：用户登录
 *
 * @author a-xin
 * @date 2023/8/9 14:01
 */
@RestController
@RequestMapping
@Api(tags = "login-用户登录")
public class SysLoginController {

    @Resource
    private SysLoginService sysLoginService;

    /**
     * 用户名密码登录
     *
     * @param loginForm 登录信息表单
     * @return 登录token信息
     */
    @PostMapping(LoginAPIConstant.LOGIN_API)
    @ApiOperation(value = "用户名密码登录")
    public Result<Map<String, Object>> login(@RequestBody LoginForm loginForm) {
        return Result.success(sysLoginService.login(loginForm));
    }

    /**
     * 手机验证码登录
     *
     * @param smsCodeLoginForm 登录信息表单
     * @return 登录token信息
     */
    @PostMapping(LoginAPIConstant.SMS_LOGIN)
    @ApiOperation(value = "手机验证码登录")
    public Result<Map<String, Object>> smsLogin(@RequestBody SmsCodeLoginForm smsCodeLoginForm) {
        return Result.success(sysLoginService.smsLogin(smsCodeLoginForm));
    }

    /**
     * 手机小程序登录
     *
     * @param appLoginForm 登录信息表单
     * @return 登录token信息
     */
    @PostMapping(LoginAPIConstant.APP_LOGIN)
    @ApiOperation(value = "手机小程序登录")
    public Result<Map<String, Object>> appLogin(@RequestBody AppLoginForm appLoginForm) {
        return Result.success(sysLoginService.appLogin(appLoginForm));
    }

    /**
     * 登出处理
     */
    @PostMapping(LoginAPIConstant.LOGOUT)
    @ApiOperation(value = "登出处理")
    public Result<String> logout() {
        sysLoginService.logout();
        return Result.success();
    }

}
